package com.billard.controller;


import com.billard.dto.ReservationDTO;
import com.billard.entity.Reservation;
import com.billard.result.Result;
import com.billard.service.ReservationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 预约控制器
 */
@RestController
@RequestMapping("/reservation")
@Tag(name = "预约管理", description = "预约相关接口")
public class ReservationController {

    @Autowired
    private ReservationService reservationService;

    @PostMapping
    @Operation(summary = "创建预约", description = "创建新的预约")
    // 创建预约可以无权限访问（客户也可以创建预约）
    @PreAuthorize("hasAnyAuthority('reservation:add', '*:*:*') or hasRole('CUSTOMER')")
    public Result<Void> createReservation(@Valid @RequestBody ReservationDTO reservationDTO) {
        reservationService.createReservation(reservationDTO);
        return Result.success();
    }

    @PostMapping("/{id}/confirm")
    @Operation(summary = "确认预约", description = "确认指定预约")
    // 确认预约需要权限
    @PreAuthorize("@securityPermission.hasPermission('reservation:confirm')")
    public Result<Void> confirmReservation(@PathVariable Long id) {
        reservationService.confirmReservation(id);
        return Result.success();
    }

    @PostMapping("/{id}/cancel")
    @Operation(summary = "取消预约", description = "取消指定预约")
    // 取消预约可以无权限访问（客户也可以取消自己的预约）
    @PreAuthorize("hasAnyAuthority('reservation:cancel', '*:*:*') or hasRole('CUSTOMER')")
    public Result<Void> cancelReservation(@PathVariable Long id) {
        reservationService.cancelReservation(id);
        return Result.success();
    }

    @GetMapping("/list")
    @Operation(summary = "查询所有预约", description = "获取所有预约信息")
    @PreAuthorize("@securityPermission.hasPermission('reservation:list')")
    public Result<List<Reservation>> getAllReservations() {
        List<Reservation> reservations = reservationService.getAllReservations();
        return Result.success(reservations);
    }

    @GetMapping("/status/{status}")
    @Operation(summary = "根据状态查询预约", description = "根据状态查询预约信息")
    @PreAuthorize("@securityPermission.hasPermission('reservation:list')")
    public Result<List<Reservation>> getReservationsByStatus(@PathVariable Integer status) {
        List<Reservation> reservations = reservationService.getReservationsByStatus(status);
        return Result.success(reservations);
    }

    @GetMapping("/table/{tableId}")
    @Operation(summary = "根据球台查询预约", description = "根据球台ID查询预约信息")
    @PreAuthorize("@securityPermission.hasPermission('reservation:list')")
    public Result<List<Reservation>> getReservationsByTableId(@PathVariable Long tableId) {
        List<Reservation> reservations = reservationService.getReservationsByTableId(tableId);
        return Result.success(reservations);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除预约", description = "删除指定预约")
    @PreAuthorize("@securityPermission.hasPermission('reservation:delete')")
    public Result<Void> deleteReservation(@PathVariable Long id) {
        reservationService.deleteReservation(id);
        return Result.success();
    }

    @GetMapping("/statistics")
    @Operation(summary = "预约统计", description = "获取预约统计数据")
    @PreAuthorize("@securityPermission.hasPermission('reservation:list')")
    public Result<Map<String, Object>> getReservationStatistics(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        return Result.success(reservationService.getReservationStatistics(startDate, endDate));
    }
}